﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class admin_zipcode : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {
            Session["tablastates"] = null;
            fillStates();
            fillcoverage();
            Session["tablazip"] = null;
            
        }
        else {
            if (Session["dscoverage"] != null) { coverage.DataSource = (DataSet)Session["dscoverage"]; coverage.DataBind(); }
            if (Session["dscoverageno"] != null) { coverageno.DataSource = (DataSet)Session["dscoverageno"]; coverageno.DataBind(); }
            if (Session["tablazip"] != null) { Table tabla = (Table)Session["tablazip"]; zipcoedes.Controls.Remove(tabla); zipcoedes.Controls.Add(tabla); }
            if (Session["tablastates"] != null) { Table tablastate = (Table)Session["tablastates"]; Panel2.Controls.Remove(tablastate); Panel1.Controls.Add(tablastate); }
        }
        
        

    }
   

    private void fillcoverage()
    {
        DataSet dscoverage = DB.getZipCoverage(" and enable = 1 ");
        DataSet dscoverageno = DB.getZipCoverage(" and enable = 0 ");
        if (coverage!= null && dscoverage!= null){
            Utility.writeError("Zipcode fillcoverageno tiene "+dscoverageno.Tables[0].Rows.Count+" con covertura");
            coverage.DataSource = dscoverage;
            coverage.DataBind();
            Session["dscoverage"] = dscoverage;
        }
        if  (coverageno!= null && dscoverageno != null)
        {
            coverageno.DataSource = dscoverageno;
            coverageno.DataBind();
            Session["dscoverageno"] = dscoverageno;
        }
        
    }
    
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        lerror2.Text = "";
        int resp = 0;
        if (!tb_state.Text.Equals("") && !tb_stateinitial.Text.Equals(""))
        {
            resp = DB.addState(tb_state.Text, tb_stateinitial.Text);
            if (resp == 1)
            {
                tb_state.Text = "";
                tb_stateinitial.Text = "";
                Panel1.Controls.RemoveAt(0);

                fillStates();


            }
            else if (resp == 3)
            {
                lerror2.Text = "State already exists!";
            }
            else
            {
                lerror2.Text = "Error. State no Added. Try Again";
            }
        }
        else
        {
            lerror2.Text = "State name and initial are requerid!";
        }
    }

     void fillStates()
    {
        Table tabla = new Table();
        DataGrid gri = new DataGrid();
        
        tabla.Width = 700;
        tabla.BorderWidth = 2;
        tabla.BorderStyle = BorderStyle.Groove;
        tabla.Attributes.Add("runat", "server");
        DataGrid dg = new DataGrid();
       // DataRow dr = new DataRow();
        TableRow tr = new TableRow();
        TableCell td1 = new TableCell();
        DataColumn dc1 = new DataColumn();
        TableCell td2 = new TableCell();
        TableCell td3 = new TableCell();
        TableCell td4 = new TableCell();
        DataSet dst = DB.GetStates();
        // cbxstate.Items.Clear();
        //cb_statecity.Items.Clear();
        // cb_statezip.Items.Clear();
        ListItem li = new ListItem();
        li.Text = "Select One";
        li.Value = "Select One";
        dd1.Items.Add(li);
        dd10.Items.Add(li);
        dd20.Items.Add(li);
        //cb_statecity.Items.Add(li);
        // cb_statezip.Items.Add(li);
        if (dst != null)
        {
           
            for (int i = 0; i <= (dst.Tables[0].Rows.Count - 1); i++)
            {

                li = new ListItem();
                li.Text = dst.Tables[0].Rows[i]["name"].ToString();
                li.Value = dst.Tables[0].Rows[i]["state_id"].ToString();
                dd1.Items.Add(li);
                dd10.Items.Add(li);
                dd20.Items.Add(li);
             

                td1 = new TableCell();
                td1.BorderStyle = BorderStyle.Dotted;
                td1.BorderWidth = 1;
                td1.BorderColor = System.Drawing.Color.LightGray;
                Label lb = new Label();

                //lb.Width = 140;
                lb.Text = li.Text;
                lb.Style.Add("text-align","left");
                HyperLink hy = new HyperLink();
                hy.Text = "<img src= '../img/uncancelled.png' width='20'>";
                hy.NavigateUrl = "http://www.bpophotos.com/admin/Delete.aspx?type=1&id=" + dst.Tables[0].Rows[i]["state_id"].ToString(); 
               
               /* ImageButton im = new ImageButton();
                im.ImageUrl = "../img/save_as.png";
                im.CommandName = "show";
                im.CommandArgument = dst.Tables[0].Rows[i]["state_id"].ToString();
                
               //im.Command += new CommandEventHandler(ImageButton1_Click);
                im.ID = "bot"+i;
                Button bt= new Button();
                bt.Click += new EventHandler(bt_Click);
                * */
                hy.Enabled = false;
                td1.Controls.Add(hy);
                td1.Controls.Add(lb);

                

               // tr.Style.Add("float", "left");
                tr.Cells.Add(td1);
               // tr.Cells.Add(td2);

                if ((i % 4) == 0)
                {


                    tr = new TableRow();
                    Table4.Rows.Add(tr);

                }

            }
            Session["tablastates"] = Table4;
            Panel1.Controls.Add(Table4);

        }
    }
    void bt_Click(object sender, EventArgs e)
    {
        RE_GenericBean rb = new RE_GenericBean();
    }

    void ln_Command(object sender, CommandEventArgs e)
    {
        RE_GenericBean r = new RE_GenericBean();
       
    }
    private void fillcounties(string source, DropDownList ddl)
    {
        ddl.Items.Clear();
        ddl.Items.Add("Select One");
        DataSet dst = DB.GetStates();
        if (dst != null)
        {




            DataSet dsc = DB.GetCounties(source);

            if (dsc != null)
            {

                if (dsc.Tables[0].Rows.Count > 0)
                {

                    for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                    {

                        ListItem list = new ListItem();
                        //nameItem = dsc.Tables[0].Rows[i]["name"].ToString();
                        list.Value = dsc.Tables[0].Rows[ii][0].ToString();
                        list.Text = dsc.Tables[0].Rows[ii]["name"].ToString();
                        ddl.Items.Add(list);




                    }

                }
            }

        }
    }
    private void fillcountiespanel(Panel panel, DropDownList cb)
    {

        DataSet dsc = DB.GetCounties(cb.SelectedItem.ToString());

        if (dsc != null)
        {
            Table tabla = new Table();
            tabla.Width = 700;
            tabla.ID = "counties";
            tabla.BorderWidth = 2;
            tabla.BorderStyle = BorderStyle.Groove;
            TableCell td1;
            TableRow tr = new TableRow();

            if (dsc.Tables[0].Rows.Count > 0)
            {

                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                {
                    string name = dsc.Tables[0].Rows[ii]["name"].ToString();
                    td1 = new TableCell();
                    td1.BorderStyle = BorderStyle.Dotted;
                    td1.BorderWidth = 1;
                    td1.BorderColor = System.Drawing.Color.LightGray;
                    Label lb = new Label();
                    lb.Width = 140;
                    lb.Style.Add("text-align", "left");
                    lb.Text = name;
                    ImageButton ln = new ImageButton();
                    ln.ImageUrl = "../img/uncancelled.png";
                    ln.Width = 20;
                    ln.CommandName = "savezipdetail";
                    ln.Enabled = false;
                    ln.Command += new CommandEventHandler(savezipdetail);
                    td1.Controls.Add(ln);
                    tr.Style.Add("float", "left");
                    td1.Controls.Add(lb);
                    tr.Cells.Add(td1);

                    if ((ii % 4) == 0)
                    {


                        tr = new TableRow();
                        tabla.Rows.Add(tr);

                    }

                }

                panel.Controls.Add(tabla);
            }

        }
    }
    protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillcountiespanel(Panel2, dd1);
    }
    protected void dd10_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillcounties(dd10.SelectedItem.ToString(), dd11);
    }

    private void fillcitiesPanel(string county, Panel panel)
    {
        DataSet dsc = DB.GetCities(county);

        if (dsc != null)
        {
            Table tabla = new Table();
            tabla.Width = 700;
            tabla.ID = "cities";
            tabla.BorderWidth = 2;
            tabla.BorderStyle = BorderStyle.Groove;
            TableCell td1;
            TableRow tr = new TableRow();

            if (dsc.Tables[0].Rows.Count > 0)
            {

                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                {
                    string name = dsc.Tables[0].Rows[ii]["name"].ToString();
                    td1 = new TableCell();
                    td1.BorderStyle = BorderStyle.Dotted;
                    td1.BorderWidth = 1;
                    td1.BorderColor = System.Drawing.Color.LightGray;
                    Label lb = new Label();
                    lb.Width = 140;
                    lb.Text = name;
                    lb.Style.Add("text-align", "left");
                    ImageButton ln = new ImageButton();
                    ln.ImageUrl = "../img/uncancelled.png";
                    ln.Width = 20;
                    ln.CommandName = "savezipdetail";
                    ln.Enabled = false;
                    ln.Command += new CommandEventHandler(savezipdetail);
                    td1.Controls.Add(ln);
                    td1.Controls.Add(lb);
                    tr.Style.Add("float","left");
                    tr.Cells.Add(td1);

                    if ((ii % 4) == 0)
                    {


                        tr = new TableRow();
                        tabla.Rows.Add(tr);

                    }

                }

                panel.Controls.Add(tabla);
            }

        }
    }


    private void fillcities(string source, DropDownList ddl)
    {
        ddl.Items.Clear();
        ddl.Items.Add("Select One");
        DataSet dst = DB.GetStates();
        if (dst != null)
        {




            DataSet dsc = DB.GetCities(source);

            if (dsc != null)
            {

                if (dsc.Tables[0].Rows.Count > 0)
                {

                    for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                    {

                        ListItem list = new ListItem();
                        //nameItem = dsc.Tables[0].Rows[i]["name"].ToString();
                        list.Value = dsc.Tables[0].Rows[ii][0].ToString();
                        list.Text = dsc.Tables[0].Rows[ii]["name"].ToString();
                        ddl.Items.Add(list);




                    }

                }
            }

        }
    }
    protected void dd11_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillcitiesPanel(dd11.SelectedValue, Panel3);
    }
    protected void dd20_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillcounties(dd20.SelectedItem.ToString(), dd21);
    }
    protected void dd21_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillcities(dd21.SelectedValue.ToString(), dd22);
    }
    protected void dd22_SelectedIndexChanged(object sender, EventArgs e)
    {
        fillzipcodesPanel(dd22.SelectedValue,zipcoedes);
    }

    private void fillzipcodesPanel(string source, Panel panel)
    {
        DataSet dsc = DB.getZibyCity(source);

        if (dsc != null)
        {
            Table tabla = new Table();
            tabla.ID = "zips";
            tabla.Width = 700;
            tabla.BorderWidth = 2;
            tabla.BorderStyle = BorderStyle.Groove;
            TableCell td1;
            TableCell td2;
            TableRow tr = new TableRow();

            if (dsc.Tables[0].Rows.Count > 0)
            {

                for (int ii = 0; ii <= (dsc.Tables[0].Rows.Count - 1); ii++)
                {
                    string name = dsc.Tables[0].Rows[ii]["zipcode"].ToString();
                    int enable = int.Parse(dsc.Tables[0].Rows[ii]["enable"].ToString());
                    td1 = new TableCell();
                   
                    Label lb = new Label();
                    lb.Width = 60;
                    lb.Style.Add("text-align", "Left");
                    lb.Text = name;
                    CheckBox ch = new CheckBox();
                    ch.Width = 20;
                    if (enable == 1) ch.Checked = true;
                    ImageButton ln = new ImageButton();
                    ln.ImageUrl = "../img/save_as.png";
                    ln.Width = 20;
                    ln.CommandName = "savezipdetail";
                    ln.Enabled = false;
                    ln.Command += new CommandEventHandler(savezipdetail);
                    ImageButton ln2 = new ImageButton();
                    ln2.ImageUrl = "../img/uncancelled.png";
                    ln2.Width = 25;
                    ln2.CommandName = "savezipdetail";
                    ln2.Command += new CommandEventHandler(savezipdetail);
                    ln2.Enabled = false;
                    td1.BorderStyle = BorderStyle.Dotted;
                    td1.BorderWidth = 1;
                    td1.BorderColor = System.Drawing.Color.LightGray;
                    td1.Controls.Add(ch);
                    td1.Controls.Add(lb);
                    td1.Style.Add("float", "center");
                    td1.Controls.Add(ln2);
                    td1.Controls.Add(ln);
                   
                  
                    tr.Cells.Add(td1);
                    
                    if ((ii % 4) == 0)
                    {


                        tr = new TableRow();
                        tabla.Rows.Add(tr);

                    }

                }

                panel.Controls.Add(tabla);
                Session["tablazip"] = tabla;
            }

        }
    }

    private void savezipdetail(object sender, EventArgs e)
    {

        ImageButton bt = (ImageButton)sender;
        Table objFila = (Table)bt.Parent.Parent;
        
    }
    public void showSelected(object sender, ImageClickEventArgs e)
    {
        //cast ImageButton on the sender object

        ImageButton imgBtn = (ImageButton)sender;
    }

    protected void savedetail(object sender, EventArgs e)
    {
        lb_mensaje.Text = "";
        ArrayList list = new ArrayList();
        int index = 0;
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;

        index = objFila.RowIndex;




        int resp = 0;
        if (coverage.Rows[index].Cells[3].FindControl("check") != null)
        {
            CheckBox ch = (CheckBox)coverage.Rows[index].Cells[3].FindControl("check");

            if (!ch.Checked)
            {
                RE_GenericBean rgzip = new RE_GenericBean();
                rgzip.intC1 = 0; //enable
                rgzip.strC1 = coverage.Rows[index].Cells[0].Text;//zipcode
                rgzip.strC2 = coverage.Rows[index].Cells[4].Text;//city
                list.Add(rgzip);
                resp = DB.updateZipcodeCoverage(list);
                fillcoverage();
            }

        }
        

    }
    protected void savedetailno(object sender, EventArgs e)
    {
        lb_mensaje.Text = "";
        int index = 0;
        ArrayList list = new ArrayList();
        ImageButton bt = (ImageButton)sender;
        GridViewRow objFila = (GridViewRow)bt.Parent.Parent;

        index = objFila.RowIndex;


       

        int resp= 0;
        if ( coverageno.Rows[index].Cells[3].FindControl("check")!= null){
            CheckBox ch= (CheckBox)coverageno.Rows[index].Cells[3].FindControl("check");
            
            if (ch.Checked){
                RE_GenericBean rgzip= new RE_GenericBean();
                rgzip.intC1= 1; //enable
                rgzip.strC1=coverageno.Rows[index].Cells[0].Text;//zipcode
                rgzip.strC2= coverageno.Rows[index].Cells[4].Text;//city
                list.Add(rgzip);
                resp = DB.updateZipcodeCoverage(list);
              
                fillcoverage();
            }
        
        }
        


    }
    protected void gv_coverage_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[0].Text = "Zip";
            e.Row.Cells[2].Text = "City";
        }
        if (e.Row.RowType == DataControlRowType.DataRow){
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[4].Visible = false;
            if (e.Row.Cells[3].Text == "1") {
                CheckBox ch = new CheckBox();
                ch.ID = "check";
                ch.Checked = true;
                ImageButton im = new ImageButton();
                im.ImageUrl = "../img/save_as.png";
                im.CommandName = "savedetail";
                im.Command += new CommandEventHandler(savedetail);
           
                e.Row.Cells[3].Controls.Add(ch);
                e.Row.Cells[3].Controls.Add(im);
            }

        }
    }

    protected void coverageno_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[0].Text = "Zip";
            e.Row.Cells[2].Text = "City";
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[1].Visible = false;
            e.Row.Cells[4].Visible = false;
            if (e.Row.Cells[3].Text == "0")
            {
                CheckBox ch = new CheckBox();
                ch.ID = "check";
                ch.Checked = false;
                ImageButton im = new ImageButton();
                im.ImageUrl = "../img/save_as.png";
                im.CommandName = "savedetailno";
                im.Command += new CommandEventHandler(savedetailno);

                e.Row.Cells[3].Controls.Add(ch);
                e.Row.Cells[3].Controls.Add(im);
            }

        }
    }
    protected void lnk_savecove_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();
        int i = 0;
        foreach (GridViewRow ROW in coverage.Rows)
        {

            string ordernum = ""; string fee = "0";
            ROW.Cells[3].FindControl("check");
            CheckBox chk = (CheckBox)ROW.Cells[3].FindControl("check");
            if (!chk.Checked) {
                RE_GenericBean rgzip = new RE_GenericBean();
                rgzip.intC1 = 0; //enable
                rgzip.strC1 = coverage.Rows[i].Cells[0].Text;//zipcode
                rgzip.strC2 = coverage.Rows[i].Cells[4].Text;//city
                list.Add(rgzip);
               
            }
            i++;
        }
        int resp = DB.updateZipcodeCoverage(list);
        if (resp == 1) { fillcoverage(); }
    }
    protected void lnk_savenocove_Click(object sender, EventArgs e)
    {
        ArrayList list = new ArrayList();
        int i = 0;
        foreach (GridViewRow ROW in coverageno.Rows)
        {

            string ordernum = ""; string fee = "0";

            CheckBox chk = (CheckBox)ROW.FindControl("check");
            if (chk.Checked)
            {
                RE_GenericBean rgzip = new RE_GenericBean();
                rgzip.intC1 = 1; //enable
                rgzip.strC1 = coverageno.Rows[i].Cells[0].Text;//zipcode
                rgzip.strC2 = coverageno.Rows[i].Cells[4].Text;//city
                list.Add(rgzip);

            }
            i++;
        }
        int resp = DB.updateZipcodeCoverage(list);
        
        if (resp == 1) { fillcoverage(); }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        RE_GenericBean rgzip = new RE_GenericBean();

    }
}